<?php
/**
 * 客户基本信息上传
 */
namespace app\adminweb\controller;
use think\Cache;
use \think\Controller;
use \think\Validate;
header("Access-Control-Allow-Origin: * ");
header("Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE");
class Article extends Pre
{
    //添加当前公告信息
    public function inserts(){
        $login = $this -> loginInfo;
        $article = new \app\common\model\Article();
        return $article -> inserts($login);
    }
    //查找当前自己的所有公告
    public function selects(){
        $data = input('param.');
        $login = $this -> loginInfo;
        if($data['id']){
            $result = \app\common\model\Article::with('accountInfo.roleInfo')
                ->where('id',$data['id'])->find();
            if($result['img']){
                $result['img'] = explode(',',$result['img']);
                for($i=0;$i<count($result['img']);$i++){
                    $val = $result['img'][$i];
                    $arr[] = 'https://show.enterlo.com'.$val;
                }
                unset($result['img']);
                $result['img'] = $arr;
                unset($arr);
            }else{
                $result['img'] = array();
            }
            $result['files'] = explode(',',$result['files']);
            if($result['files'][0] !== ''){
                for($j=0;$j<count($result['files']);$j++){
                    $vals = $result['files'][$j];
                    $arr1[] = 'https://show.enterlo.com'.$vals;
                }
                unset($result['files']);
                $result['files'] = $arr1;
                unset($arr1);
            }else{
                $result['files'] = array();
            }
            $result['date'] = date('Y-m-d H:s:i',$result['date']);
            //添加查看缓存
            $cache1 = cache($login['id']);
            if($cache1){
                if(!in_array($data['id'],$cache1)){
                    $array2 = array_merge($cache1,array($data['id']));
                    Cache($login['id'],$array2);
                }
            }else{
                Cache($login['id'],array($data['id']));
            }
            //保存当前阅读记录
            $cache = Cache('name');
            $mapinfo[] = ['id','eq',$data['id']];
            if(!$cache){
                $array[] = ['account_id' => $login['id'],'aticle_id' => $data['id']];
                Cache('name',$array,3600*24*30);
                //给当前公告添加阅读量加1
                \app\common\model\Article::where($mapinfo)->setInc('reading');
            }else{
                foreach ($cache as $value){
                    if(($value['account_id'] == $login['id']) && ($value['aticle_id'] == $data['id'])){
                        $sign = 1 ;
                    }else{
                        $sign = 0 ;
                    }
                }
                if($sign == 0){
                    $array1[] = ['account_id' => $login['id'],'aticle_id' => $data['id']];
                    $array = array_merge($cache,$array1);
                    Cache('name',$array,3600*24*30);
                    \app\common\model\Article::where($mapinfo)->setInc('reading');
                }
            }
        }else {
            $result = \app\common\model\Article::with('accountInfo.roleInfo')
                ->where('account_id', $login['id'])->order('id DESC')
                ->limit(3)->page($data['page'], $data['limit'])->select();
            foreach ($result as $value){
                $value['date'] = date('Y-m-d H:s:i',$value['date']);
                $value['img'] = explode(',',$value['img']);
                if($value['img']){
                    for($i=0;$i<count($value['img']);$i++){
                        $val = $value['img'][$i];
                        $arr[] = 'https://show.enterlo.com'.$val;
                    }
                    unset($value['img']);
                    $value['img'] = $arr;
                }else{
                    $value['img'] = array();
                }
                $value['img1'] = $value['img'][0];
                $value['files'] = explode(',',$value['files']);
                if($value['files'][0] !== ''){
                    for($j=0;$j<count($value['files']);$j++){
                        $vals = $value['files'][$j];
                        $arr1[] = 'https://show.enterlo.com'.$vals;
                    }
                    unset($value['files']);
                    $value['files'] = $arr1;
                    unset($arr);
                    unset($arr1);
                }else{
                    $value['files'] = array();
                }
            }
        }
        $count = \app\common\model\Article::where('account_id', $login['id']) ->count();
        $array = array('code'=>1,'msg' =>'123','count'=>$count,'data'=>$result);
        return $array;
    }
    //删除当前公告信息
    public function deletes(){
        $data = input('param.');
        $result = \app\common\model\Article::deletes();
        return $result;
    }
    //删除图片
    public function delImg(){
        $data = input('param.');
        $num = stripos($data['src'],'m/');
        $start = $this -> msubstr($data['src'],$num+1,200, $charset="utf-8", $suffix=false);
        if($data['id']){
            $result = \app\common\model\Article::where('id',$data['id']) -> find();
            $str = explode(',',$result['img']);
            $st = stripos($result['img'],$start);
            foreach ($str as $key => $value){
                if($value == $start){
                    unset($str[$key]);
                }
            }
                if($str){
                    foreach ($str as $val){
                        $str1 .= ','.$val;
                    }
            }
                \app\common\model\Article::where('id',$data['id']) -> setField('img',ltrim($str1,','));

            //删除当前图片
        }
        $src = '../public'.$start;
        unlink($src);
        $array = array('code'=>1,'msg' =>'删除成功');
        return $array;
    }
    //删除附件
    public function delFile(){
        $data = input('param.');
        $num = stripos($data['src'],'m/');
        $start = $this -> msubstr($data['src'],$num+1,200, $charset="utf-8", $suffix=false);
        if($data['id']){
            $result = \app\common\model\Article::where('id',$data['id']) -> find();
            $str = explode(',',$result['files']);
            $st = stripos($result['files'],$start);
            if($st){
                foreach ($str as $key => $value){
                    if($value == $start){
                        unset($str[$key]);
                    }
                }
                if($str){
                    foreach ($str as $val){
                        $str1 .= ','.$val;
                    }
                }
                \app\common\model\Article::where('id',$data['id']) -> setField('files',ltrim($str1,','));
            }
            //删除当前图片
        }
        $src = '../public'.$start;
        unlink($src);
        $array = array('code'=>1,'msg' =>'删除成功');
        return $array;
    }
    //我的公告
    public function myNotice(){
        $data = input('param.');
        $login = $this -> loginInfo;
        if($data['search']){
            $mapx[] = ['title|text','like',"%$data[search]%"];
            $re = \app\common\model\Article::where($mapx) -> column('id');
            $mapinfo[] = ['id','in',$re];
            $data['page'] = 1 ;
        }
        //全公司可见的公告
        $map1[] = ['target','eq',1];
        $map1[] = ['account_id','neq',$login['id']];
        $result1 = \app\common\model\Article::where($map1)->column('id');
        //我的上级发布的公告
        $map[] = ['b_id','eq',$login['id']];
        $allAccount = \app\common\model\Record::where($map) -> column('a_id');
        while (!empty($a_id = \app\common\model\Record::where($map)->column('a_id'))){
            $map = [];
            $allAccount = array_unique(array_filter(array_merge($allAccount,$a_id)));
            $map[] = ['b_id','in',$a_id];
        }
        $map3[] = ['account_id','in',$allAccount];
        $map3[] = ['target','eq',3];
        $result3 = \app\common\model\Article::where($map3)->column('id');
        //公司内可见
        $resu = \app\common\model\Article::where('account_id',$login['id']) -> find();
        if($resu){
                $mapin[] = ['company','eq',$resu['company']];
                $mapin[] = ['target','eq',2];
                $mapin[] = ['account_id','neq',$login['id']];
            $result4 = \app\common\model\Article::where($mapin)->column('id');
        }
        if(!$data['sign']){
            $array = array_merge($result1,$result3,$result4);
        }elseif($resu){
            $array = array_merge($result1,$result3,$result4);
        }else{
            $array = array_merge($result1,$result3);
        }
        $cache = cache($login['id']);
        if(!$data['sign']){
            if($cache){
                foreach ($cache as $value){
                    foreach ($array as $key=>$val){
                        if($value==$val){
                            unset($array[$key]);
                        }
                    }
                }
            }
            $mapinfo[] = ['id','in',$array];
            $result = \app\common\model\Article::with('accountInfo.roleInfo')
                ->where($mapinfo)->order('id DESC')->limit(3)
                -> page($data['page'],$data['limit'])->select();
            $count = \app\common\model\Article::where($mapinfo) -> count();
        }else{
            $mapinfo[] = ['id','in',$cache];
            $mapinfo[] = ['account_id','neq',$login['id']];
            $result = \app\common\model\Article::with('accountInfo.roleInfo')
                ->where($mapinfo)->order('id DESC')->limit(3)
                -> page($data['page'],$data['limit'])->select();
            $count = \app\common\model\Article::where($mapinfo) -> count();
        }
        if($result){
            foreach ($result as $value){
                $value['date'] = date('Y-m-d H:s:i',$value['date']);
                $value['img'] = explode(',',$value['img']);
                if($value['img']){
                    for($i=0;$i<count($value['img']);$i++){
                        $val = $value['img'][$i];
                        $arr[] = 'https://show.enterlo.com'.$val;
                    }
                    unset($value['img']);
                    $value['img'] = $arr;
                }else{
                    $value['img'] = array();
                }
                $value['img1'] = $value['img'][0];
            }
        }
        $array = array('code'=>1,'msg' =>'','count'=>$count,'data'=>$result);
        return $array;
    }
    //文件上传
    public function upload(){
        $data = input('param.');
        $url = "../public/static/upload/file/";
        $upload = new \app\common\logic\Upload();
        $pathName = $upload -> img('image',$url);
        foreach($pathName as $value){
            $val = stripos($value,'ic/');
            $path[] = $this -> msubstr($value,$val+2,100,
                $charset="utf-8", $suffix=false);
        }
        $array = array('code'=>1,'msg' =>'','data'=>$data,'path'=>$path);
        return $array;
    }
    //文件下载
    public function download(){
        $data = input('param.');
        $famlePath = $_GET['resum'];
        // 下载文件存放目录
        $file_dir = $data['path'];
        // 检查文件是否存在
        if (! file_exists($file_dir) ) {
            $this->error('文件未找到');
        }else {
            // 打开文件
            $file1 = fopen($file_dir, "r");
            // 输入文件标签
            Header("Content-type: application/octet-stream");
            Header("Accept-Ranges: bytes");
            Header("Accept-Length:" . filesize($file_dir));
            Header("Content-Disposition: attachment;filename=" . $file_dir);
            ob_clean();     // 重点！！！
            flush();        // 重点！！！！可以清除文件中多余的路径名以及解决乱码的问题：
            //输出文件内容
            //读取文件内容并直接输出到浏览器
            echo fread($file1, filesize($file_dir));
            fclose($file1);
            exit();
        }
    }
    //清空已读缓存
    public function reading(){
        $login = $this -> loginInfo;
        $cache = cache($login['id']);
        cache($login['id'],null);
        dump($cache);
    }
    //查看当前缓存
    public function chakan(){
        $login = $this -> loginInfo;
        $cache = cache($login['id']);
        dump($cache);
    }
    //当前未读未读公告数量
    public function unRead(){
        $login = $this -> loginInfo;
        //全公司可见的公告
        $map1[] = ['target','eq',1];
        $map1[] = ['account_id','neq',$login['id']];
        $result1 = \app\common\model\Article::where($map1)->column('id');
        //我的上级发布的公告
        $map[] = ['b_id','eq',$login['id']];
        $allAccount = \app\common\model\Record::where($map) -> column('a_id');
        while (!empty($a_id = \app\common\model\Record::where($map)->column('a_id'))){
            $map = [];
            $allAccount = array_unique(array_filter(array_merge($allAccount,$a_id)));
            $map[] = ['b_id','in',$a_id];
        }
        $map3[] = ['account_id','in',$allAccount];
        $map3[] = ['target','eq',3];
        $result3 = \app\common\model\Article::where($map3)->column('id');
        //公司内可见
        $resu = \app\common\model\Article::where('account_id',$login['id']) -> find();
        if($resu){
            if($resu['company']){
                $mapin[] = ['company','eq',$resu['company']];
                $mapin[] = ['target','eq',2];
                $mapin[] = ['account_id','neq',$login['id']];
            }
            $result4 = \app\common\model\Article::where($mapin)->column('id');
        }
        $array = array_merge($result1,$result3,$result4);
        $cache = cache($login['id']);
        if($cache){
            foreach ($cache as $value){
                foreach ($array as $key=>$val){
                    if($value==$val){
                        unset($array[$key]);
                    }
                }
            }
            $mapinfo[] = ['id','in',$array];
            $count = \app\common\model\Article::where($mapinfo) -> count();
        }else{
            $mapinfo[] = ['id','in',$array];
            $count = \app\common\model\Article::where($mapinfo) -> count();
        }
        $array = array('code'=>1,'msg' =>'','count'=>$count);
        return $array;

    }
    public function ceshi(){
        $url = '../public/static/logo/code1.jpg';
        unlink($url);
        echo '1';exit;
        $value = ['1.jpg','2.jpg','3.jpg'];
        dump($value);
        foreach ($value as $key=>$val){
            $value[$key] = 'https://show.enterlo.com/'.$val;
        }
        dump($value);
    }
    //截取字符串
    function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true)
    {
        if(function_exists("mb_substr")){
            if($suffix)
                return mb_substr($str, $start, $length, $charset)."...";
            else
                return mb_substr($str, $start, $length, $charset);
        }
        elseif(function_exists('iconv_substr')) {
            if($suffix)
                return iconv_substr($str,$start,$length,$charset)."...";
            else
                return iconv_substr($str,$start,$length,$charset);
        }
        $re['utf-8'] = "/[x01-x7f]|[xc2-xdf][x80-xbf]|[xe0-xef] 
[x80-xbf]{2}|[xf0-xff][x80-xbf]{3}/";
        $re['gb2312'] = "/[x01-x7f]|[xb0-xf7][xa0-xfe]/";
        $re['gbk'] = "/[x01-x7f]|[x81-xfe][x40-xfe]/";
        $re['big5'] = "/[x01-x7f]|[x81-xfe]([x40-x7e]|xa1-xfe])/";
        preg_match_all($re[$charset], $str, $match);
        $slice = join("",array_slice($match[0], $start, $length));
        if($suffix) return $slice."…";
        return $slice;
    }
}